/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  TouchableOpacity,
  View
} from 'react-native';
import * as WeChat from 'react-native-wechat';
class wechat extends Component {
constructor(props) {
    super(props);
    this.state = {
      apiVersion: 'waiting...',
      wxAppInstallUrl: 'waiting...',
      isWXAppSupportApi: 'waiting...',
      isWXAppInstalled: 'waiting...',
    };
  }


  async componentDidMount() {
    try {
      await WeChat.registerApp('wx3c0fca268b5898d5');
      this.setState({
        apiVersion: await WeChat.getApiVersion(),
        // wxAppInstallUrl: await WeChat.getWXAppInstallUrl(),
        isWXAppSupportApi: await WeChat.isWXAppSupportApi(),
        isWXAppInstalled: await WeChat.isWXAppInstalled()
      });
      console.log(this.state);
    } catch (e) {
      console.error(e);
    }
    console.log(WeChat);
    // console.log('getApiVersion', typeof WeChat.getApiVersion);
    // console.log('getWXAppInstallUrl', typeof WeChat.getWXAppInstallUrl);
    // console.log('sendRequest', typeof WeChat.sendRequest);
    // console.log('registerApp', typeof WeChat.registerApp);
    // console.log('sendErrorCommonResponse', typeof WeChat.sendErrorCommonResponse);
    // console.log('sendErrorUserCancelResponse', typeof WeChat.sendErrorUserCancelResponse);
    // console.log('sendAuthRequest', typeof WeChat.sendAuthRequest);
    // console.log('getWXAppInstallUrl', typeof WeChat.getWXAppInstallUrl);
    // console.log('openWXApp', typeof WeChat.openWXApp);
    // console.log('registerAppWithDescription', typeof WeChat.registerAppWithDescription);
    // console.log('isWXAppSupportApi', typeof WeChat.isWXAppSupportApi);
    // console.log('isWXAppInstalled', typeof WeChat.isWXAppInstalled);
  }


  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.text}>api版本：{this.state.apiVersion}</Text>
        <Text style={styles.text}>微信注册url：{this.state.wxAppInstallUrl}</Text>
        <Text style={styles.text}>是否支持api：{String(this.state.isWXAppSupportApi) }</Text>
        <Text style={styles.text}>是否安装微信：{String(this.state.isWXAppInstalled) }</Text>

        <TouchableOpacity onPress={this._openWXApp}>
          <Text style={styles.text}>打开微信</Text>
        </TouchableOpacity>


        <TouchableOpacity onPress={this._sendAuthRequest}>
          <Text style={styles.text}>发送验证请求</Text>
        </TouchableOpacity>

        <TouchableOpacity onPress={this._shareToTimeline}>
          <Text style={styles.text}>分享到微信朋友圈</Text>
        </TouchableOpacity>


        <TouchableOpacity onPress={this._shareToSession}>
          <Text style={styles.text}>分享到微信好友或群_链接</Text>
        </TouchableOpacity>
      </View>
    );
  }

  async _openWXApp() {
    await WeChat.openWXApp();
  }

  // async _sendAuthRequest() {
  //   await WeChat.sendAuthRequest();
  // }




  async _shareToSession() {
    let result =await WeChat.shareToSession(
      {
        title: '微信好友或群组测试链接',
        description: '手把手教React Native实战开发视频教程_总帖-出处: react native【免费】VIP视频教程 react native开发论坛',
        thumbImage: 'http://www.reactnative.vip/img/weixin_test.jpg',
        type: 'news',
        webpageUrl:'http://www.reactnative.vip/thread-161-1-1.html'
        
      }
    );
    console.log('分享到微信好友或群组是否成功：', result);
  }

   async _shareToTimeline() {
    let result =await WeChat.shareToTimeline(
      {
        title: '微信好友或群组测试链接',
        description: '手把手教React Native实战开发视频教程_总帖-出处: react native【免费】VIP视频教程 react native开发论坛',
        thumbImage: 'http://www.reactnative.vip/img/weixin_test.jpg',
        type: 'news',
        webpageUrl:'http://www.reactnative.vip/thread-161-1-1.html'
        
      }
    );
    console.log('分享到微信朋友圈是否成功：', result);
  }


  // _shareToSession = () => {

  //   WeChat.isWXAppInstalled()
  //     .then((isInstalled) => {
  //       console.log('是否已经安装了：'+isInstalled);
  //       if (isInstalled) {
  //         WeChat.shareToSession({
  //           title: '微信好友或群组测试链接',
  //           description: '手把手教React Native实战开发视频教程_总帖-出处: react native【免费】VIP视频教程 react native开发论坛',
  //           thumbImage: 'http://www.reactnative.vip/img/reactnative.png',
  //           type: 'news',
  //           webpageUrl: 'http://www.reactnative.vip/thread-161-1-1.html'
  //         })
  //           .catch((error) => {

  //             alert(error.message);
  //           });
  //       } else {

  //         alert('没有安装微信软件，请您安装微信之后再试');
  //       }
  //     });
  // }

  // _shareToTimeline = () => {
  //   WeChat.isWXAppInstalled()
  //     .then((isInstalled) => {
  //       if (isInstalled) {
  //         WeChat.shareToTimeline({
  //           title: '微信朋友圈分享测试链接',
  //           description: '手把手教React Native实战开发视频教程_总帖-出处: react native【免费】VIP视频教程 react native开发论坛',
  //           thumbImage: 'http://www.reactnative.vip/img/reactnative.png',
  //           type: 'news',
  //           webpageUrl: 'http://www.reactnative.vip/thread-161-1-1.html'
  //         })
  //           .catch((error) => {

  //             alert(error.message);
  //           });
  //       } else {

  //         alert('没有安装微信软件，请您安装微信之后再试');
  //       }
  //     });
  // }


}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  text: {
    fontSize: 20,

  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('wechat', () => wechat);
